<script setup lang="ts">
import { Handle, Position } from '@vue-flow/core';
import { NIcon, NPopover } from 'naive-ui';
import { Add, Flow, SendAlt, TaskComplete } from '@vicons/carbon';
import { ref } from 'vue';

const props = defineProps<{
  id: string;
}>();

const emit = defineEmits<{
  (e: 'add-node', nodeId: string, nodeType: string): void;
}>();

const showPopover = ref(false);

// 节点类型选项
const nodeTypeOptions = [
  {
    label: '审批节点',
    value: 'approval',
    color: '#fb923c',
    icon: TaskComplete,
  },
  {
    label: '抄送节点',
    value: 'cc',
    color: '#3296fa',
    icon: SendAlt,
  },
  {
    label: '互斥节点',
    value: 'mutex',
    color: '#10b981',
    icon: Flow,
  },
];

// 处理添加节点
function handleAddNode(nodeType: string, event: MouseEvent) {
  event.stopPropagation();
  showPopover.value = false;
  emit('add-node', props.id, nodeType);
}

// 阻止点击事件冒泡
function handleButtonClick(event: MouseEvent) {
  event.stopPropagation();
  showPopover.value = !showPopover.value;
}
</script>

<template>
  <div class="add-button-node">
    <!-- 顶部输入连接点 -->
    <Handle type="target" :position="Position.Top" class="handle" />

    <!-- 添加按钮和 Popover -->
    <NPopover v-model:show="showPopover" placement="bottom-start" trigger="click">
      <template #trigger>
        <div class="add-btn" @click="handleButtonClick">
          <NIcon :size="20" color="white">
            <Add />
          </NIcon>
        </div>
      </template>
      <div class="node-type-list">
        <div
          v-for="option of nodeTypeOptions"
          :key="option.value"
          class="node-type-item"
          @click="handleAddNode(option.value, $event)"
        >
          <div class="icon">
            <NIcon :color="option.color" :size="20">
              <component :is="option.icon" />
            </NIcon>
          </div>
          <span>{{ option.label }}</span>
        </div>
      </div>
    </NPopover>

    <!-- 底部输出连接点 -->
    <Handle type="source" :position="Position.Bottom" class="handle" />
  </div>
</template>

<style scoped>
.add-button-node {
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.add-btn {
  width: 36px;
  height: 36px;
  background: #3b82f6;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.4);
  transition: all 0.2s;
}

.add-btn:hover {
  background: #2563eb;
  transform: scale(1.15);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.6);
}

.handle {
  width: 8px;
  height: 8px;
  background: transparent;
  border: none;
}

/* Popover 内容 */
.node-type-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 150px;
}

.node-type-item {
  cursor: pointer;
  padding: 10px;
  border: 1px solid #dbdee5;
  border-radius: 10px;
  width: 170px;
  position: relative;
  display: flex;
  align-items: center;

  .icon {
    border: 1px solid #dbdee5;
    border-radius: 50%;
    padding: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 20px 0 10px;
  }

  &:hover {
    background-color: rgba(34, 130, 254, 0.1);
    box-shadow: rgba(100, 100, 111, 0.2) 0 7px 29px 0;
    color: #2282fe;
  }
}

.node-type-item:hover {
  background-color: #f3f4f6;
}

.node-type-color {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}

.node-type-item span {
  font-size: 14px;
  color: #374151;
}
</style>
